package com.jason.campusinn.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.yuandian.wanna.R;
import com.yuandian.wanna.bean.creationClothing.SurfaceMaterialBean;
import com.yuandian.wanna.utils.DisplayUtil;
import com.yuandian.wanna.adapter.beautyClothing.FabricDeatilVpAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by yuandian on 2015/5/8.
 */
public class DesignFabricDetailDialog extends Dialog implements View.OnClickListener {
    private Context mCotnext;
    @ViewInject(R.id.dialog_design_fabric_detail_iv_close)
    private ImageView mIvClose;
    @ViewInject(R.id.dialog_design_fabric_detail_vp)
    private ViewPager mVpFabric;
    @ViewInject(R.id.dialog_design_fabric_detail_indicator)
    private CirclePageIndicator mCircleIndicator;
    @ViewInject(R.id.dialog_design_fabric_detail_tv_name)
    private TextView mTvName;
    @ViewInject(R.id.dialog_design_fabric_detail_tv_english)
    private TextView mTvEnglishName;
    @ViewInject(R.id.dialog_design_fabric_detail_tv_about)
    private TextView mTvDetail;
    @ViewInject(R.id.dialog_design_fabric_detail_tv_about_two)
    private TextView mTvDeatilAbout;
    private int currentPosition;
    private Timer mTimer;
    private TimerTask mTask;
    private boolean isTaskRun = false;
    private SurfaceMaterialBean mCurrentMaterialBean;// 当前选中面料
    private List<String> mListPictures = new ArrayList<String>();
    private FabricDeatilVpAdapter adapter;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            setCurrentItem();
        }
    };

    public DesignFabricDetailDialog(Context mCotnext,SurfaceMaterialBean mCurrentMaterialBean){
        super(mCotnext, R.style.dialog_style);
        this.mCotnext = mCotnext;
        this.mCurrentMaterialBean = mCurrentMaterialBean;
        initViews();
        initContent();
    }

    private void initViews() {
        setOwnerActivity((Activity) mCotnext);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.dialogMenu);
        dialogWindow.setAttributes(lp);
        LayoutInflater inflater = LayoutInflater.from(mCotnext);
        View view = inflater.inflate(R.layout.dialog_design_fabric_detail, null);
        setContentView(view);
        setCanceledOnTouchOutside(true);
        ViewUtils.inject(this, view);
        mIvClose.setOnClickListener(this);
        mListPictures = mCurrentMaterialBean.getPictures();
        adapter = new FabricDeatilVpAdapter(mCotnext, mListPictures);
        mVpFabric.setAdapter(adapter);
        mVpFabric.setOffscreenPageLimit(mListPictures.size());
//        startTask();
        mCircleIndicator.setViewPager(mVpFabric);
        mCircleIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageSelected(int arg0) {
                currentPosition = arg0;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (arg0 == 0 && !isTaskRun) {
                    setCurrentItem();
//                    startTask();
                } else if (arg0 == 1 && isTaskRun) {
                    stopTask();
                }
            }
        });
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (adapter != null){
                    int size = adapter.getmListBitmaps().size();
                    for (int i = 0; i < size;i++){
                        adapter.getmListBitmaps().get(i).recycle();
                    }
                }
            }
        });
    }

    private void initContent(){
        String content = mCurrentMaterialBean.getMemo();//DisplayUtil.changeTextBold(content,0,5)
        mTvDetail.setText(content);
        mTvDeatilAbout.setText("");
        mTvName.setText(mCurrentMaterialBean.getMaterialName());
        mTvEnglishName.setText("");
//        startTask();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_design_fabric_detail_iv_close:
                this.dismiss();
                break;
        }

    }

    /**
     * 开启定时任务
     */
    private void startTask() {
        if (isTaskRun) {//防止多次开启
            return;
        }
        isTaskRun = true;
        mTimer = new Timer();
        mTask = new TimerTask() {
            public void run() {
                currentPosition++;
                mHandler.sendEmptyMessage(0);
            }
        };
        mTimer.schedule(mTask, 2 * 1000, 2 * 1000);// 这里设置自动切换的时间，单位是毫秒，4*1000表示4秒
    }


    /**
     * 停止定时任务
     */
    private void stopTask() {
        isTaskRun = false;
        if (mTimer != null) {
            mTimer.cancel();
        }
    }

    /**
     * 处理Page的切换逻辑
     */
    private void setCurrentItem() {
        if (currentPosition == 4) {
            currentPosition = 0;
            mVpFabric.setCurrentItem(currentPosition, false);// 取消动画
        } else {
            mVpFabric.setCurrentItem(currentPosition, true);// 取消动画
        }

    }


}
